#!/usr/bin/env bash
# rename pre-receive

zero_commit="0000000000000000000000000000000000000000"
excludeExisting="--not --all" 
# post-receive post-update不要使用上边的参数

# <old-value> SP <new-value> SP <ref-name> LF
# 没有参数,标准输入流循环输入上边数据,非零exit拒绝提交

while read oldrev newrev refname; do 
  # branch or tag get deleted 
  if [ "$newrev" = "$zero_commit" ]; then 
    continue 
  fi 
 
  # Check for new branch or tag 
  if [ "$oldrev" = "$zero_commit" ]; then 
    span=`git rev-list $newrev $excludeExisting` 
  else 
    span=`git rev-list $oldrev..$newrev $excludeExisting` 
  fi 
 
  for COMMIT in $span; 
   do
        AUTHOR_USER=`git log --format=%an -n 1 ${COMMIT}`
        COMMIT_USER=`git log --format=%cn -n 1 ${COMMIT}`
         
        if [[ ${AUTHOR_USER} != ${RTUSER} ]]; then
            echo -e "ERROR: Commit author (${AUTHOR_USER}) does not match the current Git user"
            exit 20
        fi
        
        if [[ ${COMMIT_USER} != ${RTUSER} ]]; then
            echo -e "ERROR: Commit User (${COMMIT_USER}) does not match the current Git user"
            exit 30
        fi
    done 
done 

exit 0
